package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.controller.vo.ApplyVo;
import com.controller.vo.CommisVo;
import com.dao.entity.Apply;
import com.dao.entity.Commissioner;
import com.service.IApplyService;
import com.service.ICommissionerService;
import com.utils.commons.LoginContextUtils;
import com.utils.commons.ResponseBean;
import com.utils.enums.ENUM_ROLES;
import com.utils.enums.ENUM_SUCESS;

@Controller
public class CommisController {
	
	@Autowired
	private ICommissionerService commissionerService;
	
	@Autowired
	private IApplyService applyService;
	
	@RequestMapping(value = "/getCommisList", produces = "text/html;charset=UTF-8")
	public String getCommisList(String search, Integer page, Integer size, ModelMap map) {
		Commissioner c = new Commissioner();
		if(page == null) {
			page = 1;
		}
		c.setUserName(search);
		c.setRole(ENUM_ROLES.COMMISSIONER.value);
		c.setIsApproval(ENUM_SUCESS.YES.value);
		Page<Commissioner> result = commissionerService.findUserByCondition(c, page, size, null);
		map.put("list", result.getContent());
    	map.put("page", page+1);
    	map.put("search", search);
    	map.put("total",result.getTotalPages());
		return "person/table_commissioner";
	}
	
	@RequestMapping(value = "/toCommisInfo", produces = "text/html;charset=UTF-8")
	public String toCommisInfo(ModelMap map) {
		Commissioner commissioner = commissionerService.findUserById(LoginContextUtils.getLoginUser().getId());
		map.put("commis", commissioner);
		return "person/commisInfo";
	}
	
	@RequestMapping(value = "/updateCommisInfo", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateCommisInfo(CommisVo vo, ModelMap map) {
		try {
			commissionerService.saveUser(vo.toPo());
			return ResponseBean.of(ENUM_SUCESS.SUCESS.value);
		} catch (Exception e) {
			return ResponseBean.of(ENUM_SUCESS.FAILED.value, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/applyComplete", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String applyComplete(Long applyid) {
		try {
			applyService.completeApply(applyid);
			return ResponseBean.of(ENUM_SUCESS.SUCESS.value);
		} catch (Exception e) {
			return ResponseBean.of(ENUM_SUCESS.FAILED.value, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/getApplyBank", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getApplyBankList(Long applyid) {
		try {
			StringBuffer sb = new StringBuffer();
			applyService.applySupport(applyid).forEach(e ->{
				if(sb.length() < 1) {
					sb.append(e.getBankName());
				}else {
					sb.append(",").append(e.getBankName());
				}
			});
			return ResponseBean.of(sb.toString(), ENUM_SUCESS.SUCESS.value);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBean.of(ENUM_SUCESS.FAILED.value);
		}
	}
	
	@RequestMapping(value = "/getApplyList", produces = "text/html;charset=UTF-8")
	public String getApplyList(Integer page, Integer size, ModelMap map) {
		if(page == null) {
			page = 1;
		}
		Apply app = new Apply();
		if(!ENUM_ROLES.ADMINISTRATOR.value.equals(LoginContextUtils.getLoginUser().getRole())) {
			app.setCommisId(LoginContextUtils.getLoginUser().getId());
		}
		Page<ApplyVo> result = applyService.getApplys(app, page, size);
		map.put("list", result.getContent());
		map.put("page", page+1);
		map.put("total",result.getTotalPages());
		return "person/table_apply";
	}
	
	
}
